// pages/apply/apply.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        applyType: [
            {
                title: "领子不合适",
                active: false
            },
            {
                title: "腰围不合适",
                active: false
            },
            {
                title: "胸围不合适",
                active: false
            },
            {
                title: "袖长不合适",
                active: false
            },
            {
                title: "衣长不合适",
                active: false
            },
            {
                title: "其他",
                active: false
            }
        ],
        tempArray: [],
        img_path1: "../../images/xj.jpg",
        img_path2: "../../images/xj.jpg",
        img_path3: "../../images/xj.jpg",
        index: 1

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var sub_order_id = options.sub_order_id;
        var goods_name = options.goods_name;
        that.setData({
            sub_order_id: sub_order_id,
            goods_name: goods_name
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },//上传图片
    chooseImageTap: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        that.setData({
            index:index
        })
        wx.showActionSheet({
            itemList: ['从相册中选择', '拍照'],
            itemColor: "#5673b8",
            success: function (res) {
                if (!res.cancel) {
                    if (res.tapIndex == 0) {
                        that.chooseWxImage('album')
                    } else if (res.tapIndex == 1) {
                        that.chooseWxImage('camera')
                    }
                }
            }
        })
    },//主要是用来选择图片以及接收图片路径回调的监听
    chooseWxImage: function (type) {
        var that = this;
        var index = that.data.index;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original'],
            sourceType: [type],
            success: function (res) {
                var img_path = res.tempFiles[0].path;
                if(index==1){
                    that.setData({
                        img_path1: img_path,
                    })
                }else if(index==2){
                    that.setData({
                        img_path2: img_path,
                    })
                }else if(index==3){
                    that.setData({
                        img_path3: img_path,
                    })
                }
                
                var judge_img_type = res.tempFiles[0].path.split(".")[1];
                if (judge_img_type != 'jpg') {
                    wx.showToast({
                        title: '仅支持jpg',
                        image: "../../images/warning.png"
                    })
                    return;
                }
                var params = {};
                params['type'] = 2;
                params['product_order_id'] = that.data.sub_order_id;
                params['index'] = index;
                wx.uploadFile({
                    url: app.config() + "order/uploadDiscussAftersaleImg", //仅为示例，非真实的接口地址
                    filePath: res.tempFilePaths[0],
                    name: 'upload',
                    formData: params,
                    success: function (res) {
                        var data = res.data
                        //do something
                    }
                })
            }
        })
    },//事件
    applyType: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var obj = this.data.applyType[index].title;
        var arr = this.data.tempArray;
        this.data.applyType[index].active = !this.data.applyType[index].active;
        that.setData({
            applyType: this.data.applyType,
        })
    },
    judgeArray: function (arr, obj) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == obj) {
                arr.splice(i, 1);
            }
        }
    },//判断一个元素是否在数组中
    inArray: function (arr, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] === obj) {
                return true;
            }
        }
        return false;
    },//提交
    submit: function () {
        wx.showLoading({
            title: '',
        })
        var that = this;
        var applyType = that.data.applyType;
        var after_sale_apply = "";
        for (var i = 0; i < applyType.length; i++) {
            var active = applyType[i].active;
            if (active) {
                after_sale_apply += applyType[i].title + "、"
            }
        }
        var product_order_id = that.data.sub_order_id;
        var after_sale_apply_note = that.data.after_sale_apply_note;
        var params = {};
        params['after_sale_apply'] = after_sale_apply.substr(0, after_sale_apply.length - 1);
        params['product_order_id'] = product_order_id;
        if (that.data.after_sale_apply_note) {
            params['after_sale_apply_note'] = after_sale_apply_note;
        }
        wx.request({
            url: app.config() + "order/afterSaleSubmit",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                wx.hideLoading()
                if (res.data.status == 0) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000,
                        success: function () {
                            wx.navigateBack({
                                data: 1
                            })
                        }
                    })
                }
            }
        })
    },
    upload_img: function (params) {
        wx.request({
            url: app.config() + "order/uploadDiscussAftersaleImg",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
            }
        })
    },
    textarea_event: function (e) {
        var that = this;
        var note = e.detail.value;
        that.setData({
            after_sale_apply_note: note
        })
    }
})